//=========================================================
/**@file com_hardware_h003_HardwareDriver.cpp
 * @brief
 *
 * @date 2017/6/13   19:09
 * @author Zhyioo
 * @version 1.0
 */
//---------------------------------------------------------
#define OPEN_LOGGER
#define NO_INCLUDE_OPENSSL_SOURCE
#define NO_INCLUDE_PCSC_SOURCE
#define NO_INCLUDE_USB_SOURCE
#define INCLUDE_ASYNC_SOURCE

#include <zhouyb_lib.h>
#include <zhouyb_src.h>
//---------------------------------------------------------
#include <application/driver/H003/AndroidH003Installer.h>
#include <application/driver/H003/H003Installer.cpp>
using namespace zhou_yb::application::driver;

#include <application/driver/async/JniCommandWorker.h>
using namespace zhou_yb::application::driver::async;

#include <application/tools/ICmdDriver.h>
using namespace zhou_yb::application::tools;
//---------------------------------------------------------
typedef JniCommandWorker RunnerType;
ObjectPool<RunnerType> _obj_pool;
//---------------------------------------------------------
RunnerType* _GetFD(JniInvoker& jni, jint* pFd = NULL)
{
    jint fd = 0;
    if(!JniHelper::Get(jni, "_fd", fd))
    {
        return NULL;
    }
    if(pFd != NULL)
    {
        (*pFd) = fd;
    }
    return _obj_pool.InstanceOf(fd);
}
//---------------------------------------------------------
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_hardware_h003_HardwareDriver */

#ifndef _Included_com_hardware_h003_HardwareDriver
#define _Included_com_hardware_h003_HardwareDriver
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_hardware_h003_HardwareDriver
 * Method:    nativeInit
 * Signature: (Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_com_hardware_h003_HardwareDriver_nativeInit
        (JNIEnv *env, jobject jobj, jstring sLogDir)
{
    jint fd = _obj_pool.CreateObject();
    if(fd <= 0)
        return 0;

    const char DEV_ID[] = "DEV";

    RunnerType* pRunner = _obj_pool.InstanceOf(fd);
    AndroidH003Installer::Install(*pRunner, DEV_ID);

    JniConverter cvt(env);
    string arg = cvt.get_string(sLogDir);
    if(arg.length() > 0)
    {
       CommandInstaller::InstallLOG(*pRunner, arg.c_str());
    };

    pRunner->OnWork();
    pRunner->InitJNI();
    return fd;
}

/*
 * Class:     com_hardware_h003_HardwareDriver
 * Method:    TransmitCommand
 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_hardware_h003_HardwareDriver_TransmitCommand
        (JNIEnv *env, jobject jobj, jstring sModule, jstring sCmd, jstring sArg)
{
    JniInvoker jni(env, jobj);
    JniConverter cvt(env);
    jint fd = 0;
    RunnerType* pRunner = _GetFD(jni, &fd);
    if(pRunner == NULL)
        return cvt.get_string("{CODE:'1',MSG:'fd is null'}");

    ByteBuilder sRecv(1024);
    pRunner->OnCall(env, jobj, sModule, sCmd, sArg, sRecv);
    return cvt.get_string(sRecv.GetString());
}

/*
 * Class:     com_hardware_h003_HardwareDriver
 * Method:    Prepare
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL Java_com_hardware_h003_HardwareDriver_Prepare
        (JNIEnv *env, jobject jobj)
{
    JniInvoker jni(env, jobj);
    jint fd = 0;
    RunnerType* pRunner = _GetFD(jni, &fd);
    if(pRunner == NULL)
        return JNI_FALSE;
    if(pRunner->InitENV(env, jobj))
    {
        pRunner->IsAutoHandleENV = false;
        return JNI_TRUE;
    }
    return JNI_FALSE;
}

/*
 * Class:     com_hardware_h003_HardwareDriver
 * Method:    End
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_hardware_h003_HardwareDriver_End
        (JNIEnv *env, jobject jobj)
{
    JniInvoker jni(env, jobj);
    jint fd = 0;
    RunnerType* pRunner = _GetFD(jni, &fd);
    if(pRunner != NULL)
    {
        pRunner->IsAutoHandleENV = true;
        pRunner->DisposeENV();
    }
}

#ifdef __cplusplus
}
#endif
#endif
//---------------------------------------------------------
//=========================================================